<?php	
	include('db_connection.php');

	class add_course{
		function new_trainer_add(){				
			
			$insert_trainer = "INSERT into tbl_courses (course_name,course_fee,course_description,course_from,course_to,course_timing,course_duration,course_commission,is_active) VALUES ('".addslashes($_POST['course_name'])."', '".addslashes($_POST['cousre_fee'])."', '".addslashes($_POST['course_description'])."', '".$_POST['from_course']."', '".$_POST['to_course']."', '".$_POST['course_timing']."', '".addslashes($_POST['course_duration'])."','".$_POST['course_commission']."', '".$_POST['is_active']."')";
			
			$trainer_query = mysql_query($insert_trainer) or die(mysql_error());
			
			$last_id = mysql_insert_id();
			
			$uploaddir = '../dashboard/course_ppt/'.$_POST['course_name'].'/';
			
			$is_dir = '../dashboard/course_ppt/'.$_POST['course_name'];

			$uploadimgdir = '../dashboard/course_image/'.$last_id.'/';
			$isimgdir = '../dashboard/course_image/'.$last_id;

			if(!empty($_FILES['course_image']['name'])){
				if(is_dir($isimgdir)){
					$files = glob( $uploadimgdir . '*', GLOB_MARK );       
					foreach( $files as $file )
					{
						unlink( $file );       
					}
					rmdir($isimgdir);  
				}
				if (!is_dir($isimgdir)) {
					mkdir($isimgdir);         
				}
				
				$temp_img = explode(".", $_FILES['course_image']['name']);
				$extension_img = end($temp_img);
				$newfilename_img = $last_id.'.'.end($temp_img);
				$uploadfile_img = $uploadimgdir.$newfilename_img;
				if (move_uploaded_file($_FILES['course_image']['tmp_name'], $uploadfile_img)) {
					$upload_file ="UPDATE tbl_courses SET course_image = '".$_FILES['course_image']['name']."' WHERE id='".$last_id."'" ;
					$query_file_upload = mysql_query($upload_file) or die(mysql_error());
				} 
			}

			if(!empty($_FILES['course_file']['name'])){
				if (!is_dir($is_dir)) {
					mkdir($is_dir);         
				}
				
				$temp = explode(".", $_FILES['course_file']['name']);
				$extension = end($temp);
				$newfilename = $last_id.'_0.'.end($temp);
				$uploadfile = $uploaddir.$newfilename;
				if (move_uploaded_file($_FILES['course_file']['tmp_name'], $uploadfile)) {
					$upload_file ="INSERT INTO tbl_course_ppt (course_id,file_name) VALUES ('".$last_id."','".$_FILES['course_file']['name']."')" ;
					$query_file_upload = mysql_query($upload_file) or die(mysql_error());
				} 
			}
			$success_msg = "Course Has been Sucessfully Added";
			return $success_msg;
		}

		function get_course($id){
			$select = "SELECT * FROM tbl_courses WHERE id='".$id."'";
			$query = mysql_query($select) or die(mysql_error());
			
			if(mysql_num_rows($query)){
				$results = array();
				while($row = mysql_fetch_array($query)){
					foreach ($row as $k=>$v){
					  $result->$k = $v;
					}
				 
					$results[] = $result;
				}
				return $results;    
			}
		}

		function update_course(){	
			$update = "UPDATE tbl_courses SET course_name = '".addslashes($_POST['course_name'])."',
											course_fee = '".addslashes($_POST['cousre_fee'])."',
											course_description = '".addslashes(trim($_POST['course_description']))."',
											course_from = '".$_POST['from_course']."',
											course_to = '".$_POST['to_course']."',
											course_timing = '".$_POST['course_timing']."',
											course_commission = '".$_POST['course_commission']."',
											course_duration = '".addslashes($_POST['course_duration'])."',
											is_active = '".$_POST['is_active']."',											
											update_ts = now()
											WHERE id='".$_POST['course_id']."'";
			$query_update = mysql_query($update) or die(mysql_error());

			$uploaddir = '../dashboard/profile_image/';
			$uploaddir = '../dashboard/course_ppt/'.$_POST['course_name'].'/';
			

			$select = "SELECT * FROM tbl_course_ppt where course_id='".$_POST['course_id']."'";
			$query = mysql_query($select) or die(mysql_error());
			
			$count = 0;
			if(mysql_num_rows($query)>0){
				$count = mysql_num_rows($query);
			}
			$uploadimgdir = '../dashboard/course_image/'.$_POST['course_id'].'/';
			$isimgdir = '../dashboard/course_image/'.$_POST['course_id'];
			if(!empty($_FILES['course_image']['name'])){
				if(is_dir($isimgdir)){
					$files = glob( $uploadimgdir . '*', GLOB_MARK );       
					foreach( $files as $file )
					{
						unlink( $file );       
					}
					rmdir($isimgdir); 
				}
				if (!is_dir($isimgdir)) {
					mkdir($isimgdir);         
				}
				
				$temp_img = explode(".", $_FILES['course_image']['name']);
				$extension_img = end($temp_img);
				$newfilename_img = $_POST['course_id'].'.'.end($temp_img);
				$uploadfile_img = $uploadimgdir.$newfilename_img;
				if (move_uploaded_file($_FILES['course_image']['tmp_name'], $uploadfile_img)) {
					$upload_file ="UPDATE tbl_courses SET course_image = '".$_FILES['course_image']['name']."',update_ts = now() WHERE id='".$_POST['course_id']."'" ;
					$query_file_upload = mysql_query($upload_file) or die(mysql_error());
				} 
			}

			$is_dir = '../dashboard/course_ppt/'.$_POST['course_name'];
			if(!empty($_FILES['course_file']['name'])){
				if (!is_dir($is_dir)) {
					mkdir($is_dir);         
				}
				
				$temp = explode(".", $_FILES['course_file']['name']);
				$extension = end($temp);
				$newfilename = $_POST['course_id'].'_'.$count.'.'.end($temp);
				$uploadfile = $uploaddir.$newfilename;
				if (move_uploaded_file($_FILES['course_file']['tmp_name'], $uploadfile)) {
					$upload_file ="INSERT INTO tbl_course_ppt (course_id,file_name) VALUES ('".$_POST['course_id']."','".$_FILES['course_file']['name']."')" ;
					$query_file_upload = mysql_query($upload_file) or die(mysql_error());
				} 
			}
			$success_msg = "Course Has been Sucessfully Updated";
			return $success_msg;
		}

		function del_course($course_id){
			$del_update = "UPDATE tbl_courses SET is_active = '0', update_ts = now() WHERE id='".$course_id."'";			
			$query_update = mysql_query($del_update) or die(mysql_error());
			$success_msg = "Course Has been Sucessfully Inactive";
			return $success_msg;			
		}

		function trainee_session(){
			$select_session = "SELECT count(*) FROM tbl_session WHERE trainer_id = ".$_SESSION['id']." AND trainee_id='".$_POST['trainee_season_id']."' AND course_id='".$_SESSION['course_id']."'";
			$query_session = mysql_query($select_session) or die(mysql_error());

			$session_row = mysql_fetch_array($query_session);

			$count_row = $session_row[0];
				
			$rem = count($_POST['session_check']);

			$diff = $rem-$count_row;
			
			if($diff>0){
				$insert_session = "insert into tbl_session (trainer_id,trainee_id,course_id) values('".$_SESSION['id']."','".$_POST['trainee_season_id']."','".$_SESSION['course_id']."')";
				$query_session = mysql_query($insert_session) or die(mysql_error());
				$success_msg = "Session Has been Created";
				return $success_msg;
			}
		}

	}
?>